<% facet_bucket_keys = facet.buckets.map{|t| t['key'].to_s.downcase } %>
<div class="panel panel-default">
  <div class="panel-heading">
    <h5><%= format_facet_name(name) %></h5>
  </div>
  <div class="panel-body">
    <ul class='filter'>
      <% facet.buckets.each do |term| %>
        <% next if term['key'].blank? %>

        <% term['key'] = term['key'].downcase if name == 'host_type' %>
        <% current_param = params[name.to_sym].to_s.split(',') %>
        <% active = current_param.map(&:downcase).include?(term['key'].downcase) %>
        <% formatted_term = format_term(term['key']) %>
        <li class='<%= 'active' if active %>'>
          <% if active %>
            <a href="<%= url_for(params.except(:host).merge(name.to_sym => current_param.reject{ |x| x == term['key'] }.join(','), page: nil).permit!) %>">
            	<input type='checkbox' checked>
            	<%= formatted_term %>
            </a>
          <% else %>
            <a href="<%= url_for(params.except(:host).merge(name.to_sym => current_param.push(term['key']).join(','), page: nil).permit!) %>">
            	<input type='checkbox'>
            	<%= formatted_term %>
            </a>
            <small><%= number_to_human term['doc_count'] %> projects</small>
          <% end %>
        </li>

      <% end %>

      <% if params[name.to_sym].present? %>
        <% params[name.to_sym].split(',').each do |term| %>
          <% next if term.blank? %>
          <% if !facet_bucket_keys.include?(term.downcase) %>
            <li class='active'>
              <a href="<%= url_for(params.except(:host).merge(name.to_sym => params[name.to_sym].split(',').reject{ |x| x == term }.join(','), page: nil).permit!) %>">
                <input type='checkbox' checked>
                <%= format_term(term) %>
              </a>
            </li>
          <% end %>
        <% end %>
      <% end %>
    </ul>
  </div>
</div>
